package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 流程任务查询
 * 开发者: HuXiaoDan
 * 日期: 2017/1/3 15:56
 */
@SuppressWarnings("deprecation") public class ToDoQueryFragment extends Fragment
    implements View.OnClickListener {

  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.ToDoQueryFragment.EXTRA_CONTENT";
  protected static final String EXTRA_FROM =
      "com.isunland.managesystem.ui.ToDoQueryFragment.EXTRA_FROM";

  private final int REQUEST_TYPE = 0;
  private final int REQUEST_PDEPT = 1;
  private final int REQUEST_BEGINTIME = 2;
  private final int REQUEST_ENDTIME = 3;
  private final int REQUEST_IFAPPROVAL = 4;

  private LinearLayout mllIfApproval;
  private TextView mtvIfApproval;
  private LinearLayout mllApplyPD;
  private LinearLayout mllTitle;
  private EditText metTitle;
  private LinearLayout mllFlowType;
  private TextView mtvTodoType;
  private EditText metApplyPD;
  private TextView mtvApplyBeginTime;
  private TextView mtvApplyEndTime;

  private String mtypeCode = "";//流程分类code
  private String mtypeName = "";//流程分类name
  private String mifApproveCode = "";//是否完成审批code
  private String mifApproveName = "";//是否完成审批name
  private String mapplyName = "";//申请人或部门名
  private String mtitle = "";//标题
  private String mBeginTime = "";//开始时间
  private String mEndTime = "";//结束时间
  private int mfrom;//流程任务判断来自待办，已办等哪个模块，以为查询界面不同

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity acivity = (BaseVolleyActivity) getActivity();
    if (acivity != null) {
      android.support.v7.app.ActionBar actionBar = acivity.getSupportActionBar();
      if (actionBar != null) {
        actionBar.setTitle(R.string.query);
        actionBar.setDisplayHomeAsUpEnabled(true);
      }
    }
    mfrom = getArguments().getInt(EXTRA_FROM);
    ExtraValueContent content = (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (content != null) {
      mtypeName = content.getExtName1();
      mtypeCode = content.getExtCode1();
      mifApproveName = content.getExtName2();
      mifApproveCode = content.getExtCode2();
      mapplyName = content.getExtName3();
      mtitle = content.getExtName4();
      mBeginTime = content.getExtName5();
      mEndTime = content.getExtName6();
    }
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:

        mapplyName = metApplyPD.getText().toString();
        mtitle = metTitle.getText().toString();
        ExtraValueContent contet =
            new ExtraValueContent(mtypeName, mtypeCode, mifApproveName, mifApproveCode, mapplyName,
                mtitle, mBeginTime, mEndTime);
        Intent i = new Intent();
        i.putExtra(EXTRA_CONTENT, contet);
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_todo_query, container, false);
    findViews(view);
    initData();
    return view;
  }

  private void findViews(View view) {
    mllFlowType = (LinearLayout) view.findViewById(R.id.ll_flowType);
    mtvTodoType = (TextView) view.findViewById(R.id.tv_todoType);
    ImageView mibTodoType = (ImageView) view.findViewById(R.id.ib_todoType);
    metApplyPD = (EditText) view.findViewById(R.id.et_applyPD);
    ImageView mibApplyPD = (ImageView) view.findViewById(R.id.ib_applyPD);
    mtvApplyBeginTime = (TextView) view.findViewById(R.id.tv_applyBeginTime);
    ImageView mibApplyBeginTime = (ImageView) view.findViewById(R.id.ib_applyBeginTime);
    mtvApplyEndTime = (TextView) view.findViewById(R.id.tv_applyEndTime);
    ImageView mibApplyEndTime = (ImageView) view.findViewById(R.id.ib_applyEndTime);

    mllIfApproval = (LinearLayout) view.findViewById(R.id.ll_ifApproval);
    mtvIfApproval = (TextView) view.findViewById(R.id.tv_ifApproval);
    ImageView mibIfApproval = (ImageView) view.findViewById(R.id.ib_ifApproval);
    mllApplyPD = (LinearLayout) view.findViewById(R.id.ll_applyPD);
    mllTitle = (LinearLayout) view.findViewById(R.id.ll_title);
    metTitle = (EditText) view.findViewById(R.id.et_title);

    mibIfApproval.setOnClickListener(this);
    mibTodoType.setOnClickListener(this);
    mibApplyPD.setOnClickListener(this);
    mibApplyBeginTime.setOnClickListener(this);
    mibApplyEndTime.setOnClickListener(this);
  }

  private void initData() {
    switch (mfrom) {
      case TaskListFragment.MY_TODO_LIST:
        //待办
        mllIfApproval.setVisibility(View.GONE);
        mllTitle.setVisibility(View.GONE);
        break;
      //case TaskListFragment.MY_DONE_LIST:
        //已办
      //mllTitle.setVisibility(View.GONE);
      //break;
      case TaskListFragment.MY_REQUEST:
        //我的请求
        mllIfApproval.setVisibility(View.GONE);
        mllApplyPD.setVisibility(View.GONE);
        break;
      case TaskListFragment.MY_COMPLETED:
        //我的办结
        mllFlowType.setVisibility(View.GONE);
        mllIfApproval.setVisibility(View.GONE);
        mllApplyPD.setVisibility(View.GONE);
        break;
      default:
        break;
    }

    mtvTodoType.setText(mtypeName);
    mtvIfApproval.setText(mifApproveName);
    metApplyPD.setText(mapplyName);
    metTitle.setText(mtitle);
    mtvApplyBeginTime.setText(mBeginTime);
    mtvApplyEndTime.setText(mEndTime);
  }

  @Override public void onClick(View view) {
    switch (view.getId()) {

      case R.id.ib_todoType:
        //流程分类
        showDialog(REQUEST_TYPE);
        break;
      case R.id.ib_ifApproval:
        //是否完成审批
        showDialog(REQUEST_IFAPPROVAL);
        break;
      case R.id.ib_applyPD:
        //申请人或部门
        Intent i = new Intent(getActivity(), PDeptListActivity.class);
        startActivityForResult(i, REQUEST_PDEPT);
        break;
      case R.id.ib_applyBeginTime:
        //申请开始时间
        showDialog(REQUEST_BEGINTIME);
        break;
      case R.id.ib_applyEndTime:
        //申请结束时间
        showDialog(REQUEST_ENDTIME);
        break;
      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment mdialog = null;
    switch (requestCode) {
      case REQUEST_IFAPPROVAL:
        mdialog = new ChooseIfAllDialogFragment();
        break;
      case REQUEST_TYPE:
        //流程分类
        mdialog = FlowTypeDialogFragment.newInstance(mfrom, mifApproveCode);
        break;
      case REQUEST_BEGINTIME:
        mdialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_ENDTIME:
        mdialog = new BaseYMDTimeDialogFragment();
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (mdialog != null) {
      mdialog.setTargetFragment(this, requestCode);
      mdialog.show(fm, "");
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_TYPE) {
      //流程分类
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mtypeCode = customer.getId();
      mtypeName = customer.getName();
      mtvTodoType.setText(mtypeName);
    }

    if (requestCode == REQUEST_IFAPPROVAL) {
      //是否审批完成
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(ChooseIfAllDialogFragment.EXTRA_VALUE);
      mifApproveName = customer.getName();
      if (TextUtils.isEmpty(customer.getId())) {
        mifApproveCode = "-1";
      } else {
        if ("T".equalsIgnoreCase(customer.getId())) {
          mifApproveCode = "0";
        } else if ("F".equalsIgnoreCase(customer.getId())) {
          mifApproveCode = "1";
        }
      }
      mtvIfApproval.setText(mifApproveName);//数据状态
    }

    if (requestCode == REQUEST_PDEPT) {
      //申请人
      //            CustomerDialog customer = (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      //            mapplyName = customer.getName();// 申请人人姓名
      //            metApplyPD.setText(mapplyName);
      //申请人
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(AppointerListFragment.EXTRA_VALUE);
      mapplyName = customer.getName();// 申请人人姓名
      metApplyPD.setText(mapplyName);
    }

    if (requestCode == REQUEST_BEGINTIME) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mBeginTime = MyDateUtil.parseTimeToString(date, MyDateUtil.TYPE_DATE);
      mtvApplyBeginTime.setText(mBeginTime);
    }

    if (requestCode == REQUEST_ENDTIME) {
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.parseTimeToString(date, MyDateUtil.TYPE_DATE);
      mtvApplyEndTime.setText(mEndTime);
    }
  }

  public static ToDoQueryFragment newInstance(ExtraValueContent content, int from) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    args.putInt(EXTRA_FROM, from);
    ToDoQueryFragment fragment = new ToDoQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
